package base.sys.service;


import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import base.core.spring.BaseService;
import base.core.util.Beans;
import base.sys.api.enetity.SysMenu;
import base.sys.api.vo.SysMenuTree;
import base.sys.mapper.SysMenuMapper;

/**
* @Description 菜单权限表
* @Author elegant
* @CreatDate 2022/1/8 16:27
*/
@Service
public class SysMenuService extends BaseService<SysMenuMapper, SysMenu> {

    public List<SysMenuTree> getSysMenuTree() {
        Map<Integer, SysMenuTree> sysMenuOPMap = this.list().stream().map(baseSysMenu -> Beans.copy(baseSysMenu, SysMenuTree.class))
                                                     .collect(Collectors.toMap(SysMenuTree::getId, Function.identity()));
        sysMenuOPMap.values().forEach(sysMenuOP -> {
            SysMenuTree parent = sysMenuOPMap.get(sysMenuOP.getParentId());
            if (parent != null) {
                parent.getChildren().add(sysMenuOP);
            }
        });
        return sysMenuOPMap.values().stream().filter(sysMenuOP -> sysMenuOP.getParentId() == null).toList();
    }
}
